package common.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    public static String longToStr(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }


    public static String strToLong(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    private static String longToDate_ymd(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }

    /**
     * 返回上个月的今天
     *
     * @return
     */
    private static String currentDayBackMonth() {
        DateTime dateTime = DateUtil.lastMonth();
        String s = dateTime.toString(new SimpleDateFormat("yyyy-MM-dd"));
        return s;
    }


    private static String parseLocalDateTime(LocalDateTime localDateTime) {
        String time = "";
        DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        time = localDateTime.format(inFormat);
        return time;
    }

    private static String longToDate_Hms(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }


    private static long stringToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    private static long stringToDateH(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }


    private static boolean equalDate(Date date1, Date date2, String type) {
        boolean flag = true;
        int date1Year = DateUtil.year(date1);
        int date1Month = DateUtil.month(date1);
        int date1Day = DateUtil.dayOfMonth(date1);

        int date2Year = DateUtil.year(date2);
        int date2Month = DateUtil.month(date2);
        int date2Day = DateUtil.dayOfMonth(date2);

        if ("day".equals(type)) {
            flag = date1Year == date2Year && date1Month == date2Month && date1Day == date2Day;
        } else if ("month".equals(type)) {
            flag = date1Year == date2Year && date1Month == date2Month;
        } else if ("year".equals(type)) {
            flag = date1Year == date2Year;
        } else {
            flag = false;
        }
        return flag;
    }
}
